/**
 * 
 */
let workLis = document.querySelectorAll('.work-right')
let arrLis = document.querySelectorAll('.option')
for (let i = 0; i < arrLis.length; i++) {
   arrLis[i].addEventListener('click', function () {
      document.querySelector('.slide').classList.remove('slide')
      this.classList.add('slide')
      document.querySelector('.block').classList.remove('block')
      workLis[i].classList.add('block')
   })
}
let del = document.querySelectorAll('[name=del]')
// del.addEventListener('click',()=>{})
let tbody = document.querySelector('#book')
tbody.addEventListener('click', (e) => {
   if (e.target.tagName === 'BUTTON') {
      if (e.target.dataset.id === '0') {
         alert('该书已在您书单中')
         return
      }
      // console.log(e.target.dataset.id);
      del[e.target.dataset.id].click()
   }
   e.target.disabled = true
  
   // let clock ={
   //    settime:1
   // }
   // localStorage.setItem('time1',JSON.stringify(clock)) 
   // return  function time(t){
   //    setInterval(()=>{
   //      e.target.disabled=true
   //    },t)
   // }
})
// let obj = JSON.parse(localStorage.getItem('time1'))
// window.addEventListener('load',()=>{
//    console.log(obj.settime)
//    tbody.click()
//    time(obj.settime)
// })


//表单验证
function verifyPhone() {
   let reg = /^1{1}\d{10}$/
   if (reg.test(phone.value)) {
      return true
   }
   else {
      return false
   }
}
let phone = document.querySelector('[name=phone]')

let erweima = document.querySelector('.erweima')
erweima.scrollTop = 200
let close = document.querySelector('i')
close.addEventListener('click', function () {
   close.parentNode.style.display = 'none'
})
let id = document.querySelector('[name=id]')
let btnfirst = document.querySelector('#first')
btnfirst.addEventListener('click', (e) => {
   if (id.value === '') {
      alert('学号不能为空')
      e.preventDefault()
      return
   }
   if (verifyPhone()) {
      alert('修改成功')
   } else {
      alert('请输入正确电话号码')
      e.preventDefault()
      return
   }

})
//本地存储
let tb = document.querySelector('#tb')
let modal = document.querySelector('.update-form')
modal.addEventListener('submit', function (e) {
   let obj = serialize(this, { hash: true })
   localStorage.setItem('obj', JSON.stringify(obj))
})
window.addEventListener('load', () => {
   let object = JSON.parse(localStorage.getItem('obj'))
   tb.innerHTML = ` 
  <tr>
  <th scope="row"></th>
  <td>${object.id}</td>
  <td>${object.uname}</td>
  <td>${object.age}</td>
  <td>${object.sex}</td>
  <td>${object.school}</td>
  <td>${object.phone}</td>
  <td>${object.province + object.city + object.area}</td>
  </tr>`
})
// 删除本地存储
let quit = document.querySelector('#quit1')
quit.addEventListener('click',()=>{
   localStorage.removeItem('obj')
})

//图书归还
let back = document.querySelectorAll('[name=back]')
// del.addEventListener('click',()=>{})
let reback = document.querySelectorAll('#backbook')
for(let i =0;i<reback.length;i++){
   reback[i].addEventListener('click', (e) => {
      if (e.target.tagName === 'BUTTON') {
         if (e.target.dataset.num === '0') {
            alert('此书无需归还')
            return
         }
         // console.log(e.target.dataset.num);
         alert('归还成功')
         back[e.target.dataset.num].click()
      }
   })
}


